package no.ntnu.fp.model;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Person extends AbstractModel {
	static Map<Integer, Person> instances = new HashMap<Integer, Person>();
	static private int currentUser = 0;

	public String name;
	public String email;
	public String username;

	public List<Message> getMyMessages() {
		return Message.getAllTo(this);
	}

	private PropertyChangeSupport propChangeSupp;
	private static int COUNTER = 1;

	public final static String NAME_PROPERTY_NAME = "name";

	public final static String EMAIL_PROPERTY_NAME = "email";

	public static Map<Integer, Person> getAll() { //
		return instances;
	}
	
	public static List<Person> all() {
		List<Person> all = new ArrayList<Person>();
		for (Map.Entry<Integer, Person> entry : instances.entrySet()) {
			Person person = entry.getValue();
			all.add(person);
		}
		return all;
	}

	public Person() {
		name = "";
		email = "";
		id = requestId();
		propChangeSupp = new PropertyChangeSupport(this);
	}

	public Person(String name, String email, Date dateOfBirth) {
		this();
		this.name = name;
		this.email = email;
	}

	public void setName(String name) {
		String oldName = this.name;
		this.name = name;
		PropertyChangeEvent event = new PropertyChangeEvent(this,
				NAME_PROPERTY_NAME, oldName, name);

		propChangeSupp.firePropertyChange(event);
	}

	public void setEmail(String email) {
		String oldEmail = this.email;
		this.email = email;
		PropertyChangeEvent event = new PropertyChangeEvent(this,
				EMAIL_PROPERTY_NAME, oldEmail, this.email);
		propChangeSupp.firePropertyChange(event);
	}

	public String getName() {
		return name;
	}

	public String getEmail() {
		return email;
	}

	public int getId() {
		return id;
	}

	public void addPropertyChangeListener(PropertyChangeListener listener) {
		propChangeSupp.addPropertyChangeListener(listener);
	}//

	public void removePropertyChangeListener(PropertyChangeListener listener) {
		propChangeSupp.removePropertyChangeListener(listener);
	}

	public boolean equals(Object obj) {
		if (super.equals(obj))
			return true;

		if (obj == null)
			return false;

		if (obj.getClass() != this.getClass())
			return false;

		Person other = (Person) obj;

		return other.id == id;
	}

	public String toString() {
		String s = "Name: " + getName() + "; ";
		s += "Email: " + getEmail() + "; ";
		return s;
	}

	@Override
	public Map<String, Object> pack() {
		Map<String, Object> m = createAttributeMap();
		m.put("name", name);
		m.put("email", email);
		m.put("username", username);
		return m;
	}

	@Override
	public void writeAttribute(String attribute, Object value) {
		if (attribute.equals("name")) {
			name = (String) value;
		} else if (attribute.equals("email")) {
			email = (String) value;
		} else if (attribute.equals("username")) {
			username = (String) value;
		}
	}

	public static Person get(int id) {
		if (instances.containsKey(id)) {
			return instances.get(id);
		} else {
			return null;
		}
	}

	public void register(int id) {
		instances.put(id, this);
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsername() {
		return username;
	}

	public static void setCurrentUser(Person user) {
		Person.currentUser = user.id;
		if(!instances.containsValue(user)){
			instances.put(user.id, user);
			//Legg inn alle appointments til brukeren her:
			
		}
	}

	public static void setCurrentUserId(int userId) {
		Person.currentUser = userId;
	}

	public static Person getCurrentUser() {
		return get(currentUser);
	}

//	public static Person exampleCurrentUser() {
//		Person bjarne = new Person("Bjarne �s", "bjarne.aas@firmax.com",
//				new java.sql.Date(1989, 1, 27));
//
//		Person.setCurrentUser(bjarne);
//		return bjarne;
//	}

	public static void makeExamplesUsers() {
		{
			Person aPerson = new Person("Pelle Torpedo",
					"pellemann@firmax.com", new java.sql.Date(1989, 1, 28));
			aPerson.register(aPerson.id);
		}

		{
			Person aPerson = new Person("Roger Rogersen",
					"rogersen@firmax.com", new java.sql.Date(1979, 4, 27));
			aPerson.register(aPerson.id);
		}
		
		{
			Person aPerson = new Person("Bjarnemann Bjarnessen",
					"bjarnemann@firmax.com", new java.sql.Date(1979, 2, 27));
			aPerson.register(aPerson.id);
		}
		
		{
			Person aPerson = new Person("Torgeir Balle",
					"balle@firmax.com", new java.sql.Date(1979, 2, 27));
			aPerson.register(aPerson.id);
		}
	}
}
